package com.liaoye.hellocustomview.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.liaoye.hellocustomview.R;

public class ShadowLayerView extends View {
    private Paint mPaint = new Paint();
    private Bitmap mBitmap;
    private boolean isShadow;


    public ShadowLayerView(Context context) {
        super(context);
    }

    public ShadowLayerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        setLayerType(LAYER_TYPE_SOFTWARE, null);
        mPaint.setColor(Color.BLACK);
        mPaint.setTextSize(25);
        mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.dog);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (isShadow) {
            mPaint.setShadowLayer(1, 10, 10, Color.GRAY);
        } else {
            mPaint.clearShadowLayer();
        }

        canvas.drawColor(Color.WHITE);
        canvas.drawText("你好", 100, 100, mPaint);
        canvas.drawCircle(300, 100, 50, mPaint);
        canvas.drawBitmap(mBitmap, null, new Rect(500, 50, 500 + mBitmap.getWidth(), 50 + mBitmap.getHeight()), mPaint);
    }

    public void setShadow(boolean shadow) {
        isShadow = shadow;
        postInvalidate();
    }
}
